//
//  BalloonMarker.swift
//  ChartsDemo-Swift
//
//  Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
//  A port of MPAndroidChart for iOS
//  Licensed under Apache License 2.0
//
//  https://github.com/danielgindi/Charts
//

import Foundation
import Charts

open class LineMarker: MarkerImage
{
    open var color: UIColor
    open var arrowSize = CGSize(width: 15, height: 11)
    open var font: UIFont
    open var textColor: UIColor
    open var insets: UIEdgeInsets
    open var minimumSize = CGSize()
    
    private var maxWidth: CGFloat = 0.0
    private var images: Array<UIImage>?
    fileprivate var label: String?
    fileprivate var _labelSize: CGSize = CGSize()
    fileprivate var _paragraphStyle: NSMutableParagraphStyle?
    fileprivate var _drawAttributes = [NSAttributedString.Key : AnyObject]()
    
    public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
    {
        self.color = color
        self.font = font
        self.textColor = textColor
        self.insets = insets
        
        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .left
        super.init()
    }
    
    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        
        
        let size = CGSize(width: (self.image?.size.width ?? 0) + _labelSize.width + 10, height: _labelSize.height * 1.5)
        
        if (UIScreen.main.bounds.size.width - point.x) < size.width / 2 {
            
            return CGPoint(x: -size.width / 2, y: 0)
            
        }
        
        return CGPoint(x: 0, y: 0)
        
    }
    
    open override func draw(context: CGContext, point: CGPoint)
    {
        guard let label = label else { return }
        
        let offset = self.offsetForDrawing(atPoint: point)
        
        var imagesize = CGSize.zero
        
        if self.images != nil {

            imagesize = self.images![0].size

        }
        
        let size = CGSize(width: imagesize.width + _labelSize.width + 10, height: _labelSize.height)
        
        var rect = CGRect(
            origin: CGPoint(
                x: point.x + offset.x,
                y: point.y + offset.y),
            size: size)
        
        rect.origin.x -= size.width / 2.0
        
        rect.origin.y -= size.height
        
        context.saveGState()
        
        context.setFillColor(color.cgColor)
        
        let path = CGMutablePath.init()
        
        path.addRoundedRect(in: rect, cornerWidth: 2, cornerHeight: 2)
        
        context.addPath(path)
        
        context.fillPath()
        
        if offset.y > 0 {
            rect.origin.y += self.insets.top + arrowSize.height
        } else {
            rect.origin.y += self.insets.top
        }
        
        rect.size.height -= self.insets.top + self.insets.bottom
        
        UIGraphicsPushContext(context)
        
        label.draw(in: CGRect(x: rect.origin.x + 5 + imagesize.width, y: rect.origin.y, width: rect.width, height: rect.height), withAttributes: _drawAttributes)
        
        if self.images != nil {
            
            for i in 0 ..< self.images!.count {
                
                let im = self.images![i]
                
                let y = (im.size.height + 4) * CGFloat(i)
                
                context.draw(im.cgImage!, in: CGRect(x: rect.origin.x + 2, y: rect.origin.y + 18 + y, width: im.size.width, height: im.size.height))
                
            }
            
        } //图表绘制好了
        
        UIGraphicsPopContext()
        
        context.restoreGState()
    }
    
    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        //        self.image =
        
        
        if let chart = self.chartView {
            
            var ims: Array<UIImage> = []
            
            for item in chart.data!.dataSets {
                
                let i = item as! LineChartDataSet
                
                 let color = i.color(atIndex: 0)
                
                ims.append(color.image(size: CGSize(width: 10, height: 10)))
            }
            
            images = ims
        }
        
//        var month = 0
        
        let month = self.chartView!.data!.dataSets[highlight.dataSetIndex].entryIndex(entry: entry)
        
        let monthTitle = String(month + 1) + "月"
        
        let ticketSet = self.chartView!.data!.dataSets[0] as! LineChartDataSet
        
        let tickets = ticketSet.label! + ":" + String(ticketSet.values[month].y)
        
        maxWidth = tickets.size(with: self.font).width > maxWidth ? tickets.size(with: self.font).width : maxWidth
    
        let backSet = self.chartView!.data!.dataSets[1] as! LineChartDataSet
        
        let backs = backSet.label! + ":" + String(backSet.values[month].y)
        
        maxWidth = backs.size(with: self.font).width > maxWidth ? backs.size(with: self.font).width : maxWidth
        
        let backnotpaySet = self.chartView!.data!.dataSets[2] as! LineChartDataSet
        
        let backnotpay = backnotpaySet.label! + ":" + String(backnotpaySet.values[month].y)
        
        maxWidth = backnotpay.size(with: self.font).width > maxWidth ? backs.size(with: self.font).width : maxWidth
        
        setLabel(monthTitle + "\n" + tickets + "\n" + backs + "\n" + backnotpay)
        
    }
    
    open func setLabel(_ newLabel: String)
    {
        label = newLabel
        
        _drawAttributes.removeAll()
        _drawAttributes[.font] = self.font
        _drawAttributes[.paragraphStyle] = _paragraphStyle
        _drawAttributes[.foregroundColor] = self.textColor
        
        _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero
        
        var size = CGSize()
        size.width = _labelSize.width + self.insets.left + self.insets.right
        size.height = _labelSize.height + self.insets.top + self.insets.bottom
        size.width = max(minimumSize.width, size.width)
        size.height = max(minimumSize.height, size.height)
        self.size = size
    }
}

